﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using log4net;

public partial class Pages_ShowFlower : System.Web.UI.Page
{

    private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    static   BLLSanPham bllSanPham;
   static DataTable datalist = new DataTable();
   static DataRow dr;
    protected void Page_Load(object sender, EventArgs e)
   {
       datalist = (DataTable)HttpContext.Current.Session["GioHang"];
       if (!IsPostBack)
       {
         //Add các cột vào table gio hang
            //datalist.Rows.Clear();
            //datalist.Columns.Clear();
            //datalist.Columns.Add("Masp", typeof(string));
            //datalist.Columns.Add("TenSp", typeof(string));
            //datalist.Columns.Add("SoLuong", typeof(int));
            //datalist.Columns.Add("DONGIA", typeof(int));
            //datalist.Columns.Add("TONGGIA", typeof(int));
            //datalist.Columns.Add("CONLAI", typeof(int));

           
        }
        
    }
    
    [WebMethod]
    public static string AddFlower(string item)
    {
        datalist = (DataTable)HttpContext.Current.Session["GioHang"];  
        bllSanPham = new BLLSanPham();
        SanPham dtoSanPham = new SanPham();
        dtoSanPham = bllSanPham.iSanPham.selectbyid(item);       

        for (int i = 0; i < datalist.Rows.Count; i++)
        {
            if (datalist.Rows[i]["Masp"].ToString().Trim() == item)
            {
                datalist.Rows[i]["SoLuong"] = int.Parse(datalist.Rows[i]["SoLuong"].ToString())+1;
                datalist.Rows[i]["TONGGIA"] = (int)datalist.Rows[i]["SoLuong"] * (int)datalist.Rows[i]["DONGIA"];
                datalist.Rows[i]["CONLAI"] = dtoSanPham.Soluong - (int)datalist.Rows[i]["SoLuong"];            
                HttpContext.Current.Session["Gia"] = (int)HttpContext.Current.Session["Gia"] + dtoSanPham.Dongia;
                return datalist.Rows.Count.ToString();               
            }        
        }

        object[] row = { dtoSanPham.Masp, dtoSanPham.Tensp, 1, dtoSanPham.Dongia, dtoSanPham.Dongia,dtoSanPham.Soluong- 1 }; 

      //  dr["Masp"] = dtoSanPham.Masp;
        //dr["TenSp"] = dtoSanPham.Tensp;
        //dr["SoLuong"] = 1;
        //dr["DONGIA"] = dtoSanPham.Dongia;
        //dr["TONGGIA"] = (int)dr["DONGIA"] * (int)dr["SoLuong"];
        //dr["CONLAI"] = dtoSanPham.Soluong - (int)dr["SoLuong"];

        datalist.Rows.Add(row);
        
       HttpContext.Current.Session["GioHang"] = datalist;
       HttpContext.Current.Session["Gia"] =(int)HttpContext.Current.Session["Gia"] + dtoSanPham.Dongia;

      return datalist.Rows.Count.ToString();
      // return item;
    }


    [WebMethod]
    public static string RemoveFlower(string item)
    {
        datalist = (DataTable)HttpContext.Current.Session["GioHang"];
        bllSanPham = new BLLSanPham();
        SanPham dtoSanPham = new SanPham();

        for (int i = 0; i < datalist.Rows.Count;i++ )
        {
            if (datalist.Rows[i]["Masp"].ToString().Trim() == item)
            {
                string items = datalist.Rows[i]["Masp"].ToString().Trim();
                int SoLuong = (int)datalist.Rows[i]["SoLuong"];
                dtoSanPham = bllSanPham.iSanPham.selectbyid(items);
                datalist.Rows.RemoveAt(i);
                HttpContext.Current.Session["GioHang"] = datalist;
                HttpContext.Current.Session["Gia"] =(int)HttpContext.Current.Session["Gia"] - dtoSanPham.Dongia* SoLuong;

                //    datalist.Rows.Remove(dr);
            }
        }
        return datalist.Rows.Count.ToString();
    }

    [WebMethod]
    public static string test()
    {
        bllSanPham = new BLLSanPham();
        SanPham dtoSanPham = new SanPham();
        string items = null;
        string id = null;
        //  datalist.Rows.Clear();
        //  datalist = (DataTable)HttpContext.Current.Session["GioHang"];
        if (((DataTable)HttpContext.Current.Session["GioHang"]) != null)
        {
            for (int i = 0; i < ((DataTable)HttpContext.Current.Session["GioHang"]).Rows.Count; i++)
            {
                id = ((DataTable)HttpContext.Current.Session["GioHang"]).Rows[i]["Masp"].ToString();
                dtoSanPham = bllSanPham.iSanPham.selectbyid(id.Trim());
                // Add Items for ShoppingCart
                items = items + @"<div class='itemshop icart'> <div class='divrm'><a style='visibility:visible' id='" + dtoSanPham.Masp + "' onclick='remove(this)' class='remove " + dtoSanPham.Masp + "' >&times;</a></div> <a href='ProductDetails.aspx?ID=" + dtoSanPham.Masp + "'> <img id='imgc' src='../Images/total/" + dtoSanPham.Hinhsp + "'  alt='' /></a></div>";
                // end
            }
            return items;
        }

        return ((DataTable)HttpContext.Current.Session["GioHang"]).Rows.Count.ToString();
    }

    [WebMethod]
    public static string Clear()
    {
        ((DataTable)HttpContext.Current.Session["GioHang"]).Rows.Clear();
        HttpContext.Current.Session["Gia"] = 0;
        datalist.Rows.Clear();
        return "0K";
    }

    [WebMethod]
    public static string Gia()
    {
        return HttpContext.Current.Session["Gia"].ToString();
    }

    [WebMethod]
    public static string SoLuong()
    {        
        return ((DataTable)HttpContext.Current.Session["GioHang"]).Rows.Count.ToString();
    }
}